import sys

from lib.ana.properties_ana import PropertiesAna
from lib.init.application_init import ApplicationInit
from lib.init.logger_init import LoggerInit
from lib.utils.file_util import FileUtil
from lib.utils.print_util import PrintUtil


class SettingInit(object):
    file_path = None
    root_path = FileUtil.get_root_path()

    def __init__(self):
        pass

    def start(self):
        _path = SettingInit.file_path
        if _path:
            path_list = _path.split(",")
        else:
            PrintUtil.start("start application can not find [application, logger] properties ...")
            sys.exit(0)
        if path_list:
            root = SettingInit.root_path
            for path in path_list:
                path = root + path
                self.init_property(path)
        else:
            PrintUtil.start("start application can not find [application, logger] properties ...")
            sys.exit(0)

    def init_property(self, path):
        try:
            PrintUtil.start("init setting file ,file path is %s" % path)
            prop = PropertiesAna(path)
        except FileExistsError:
            PrintUtil.start("init properties fail, cannot find %s" % path)
            sys.exit(0)
        if "application" in path:
            self.init_application(prop)
        elif "logger" in path:
            self.init_logger(prop)

    def init_application(self, prop):
        ApplicationInit.set_prop(prop)

    def init_logger(self, prop):
        LoggerInit.set_prop(prop)
